package com.guli.eduservice.controller;


import com.guli.commonutils.Result;
import com.guli.eduservice.client.VodClient;
import com.guli.eduservice.entity.EduVideo;
import com.guli.eduservice.service.EduVideoService;
import com.guli.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return Result.ok();
    }

    //删除小节
    @DeleteMapping("{id}")
    public Result deleteVideo(@PathVariable String id) {
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            Result result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败，熔断器...");
            }
        }
        eduVideoService.removeById(id);
        return Result.ok();
    }






    @PostMapping("updateChapter")
    public Result updateVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return Result.ok();
    }

    //小节id查询
    @GetMapping("getVideoInfo/{VideoId}")
    public Result getChapterInfo(@PathVariable String VideoId) {
        EduVideo eduVideo = eduVideoService.getById(VideoId);
        return Result.ok().data("chapter", eduVideo);
    }

//    @GetMapping("getVideo")
//    public Result getVideo(@PathVariable String id) {
//        EduVideo eduVideo = eduVideoService.getById(id);
//        return Result.ok().data("video", eduVideo);
//    }

}

